import { toSuccess, useEntityManager } from "@sker/core"
import { AutoUser } from "@sker/entities"
import { useData } from "@sker/runtime"
import { z } from "zod"

export default async () => {
    const input = z.object({
        account: z.string(),
        password: z.string(),
        uuid: z.string(),
        token: z.string()
    })
    const body = input.parse(useData())
    return await useEntityManager(async m => {
        const user = await m.findOne(AutoUser, { where: { token: body.token } })
        if (!user) return toSuccess(false)
        const setting = user.setting || {}
        await m.update(AutoUser, user.id, {
            setting: {
                ...setting,
                baiyun: {
                    account: body.account,
                    password: body.password
                }
            }
        })
        return toSuccess(true)
    })
}